import { Sequelize } from 'sequelize';

// 创建数据库连接
const sequelize = new Sequelize({
  database: process.env.DB_NAME || 'perf',
  username: process.env.DB_USER || 'root',
  password: process.env.DB_PWD || '',
  host: process.env.DB_IP?.split(':')[0] || 'localhost',
  port: parseInt(process.env.DB_IP?.split(':')[1] || '3306'),
  dialect: 'mysql',
  dialectModule: require('mysql2'),
  define: {
    charset: 'utf16',
    collate: 'utf16_general_ci',
    timestamps: true,
  },
});

// 测试连接
async function testConnection() {
  try {
    await sequelize.authenticate();
    console.log('数据库连接成功!');
  } catch (error) {
    console.error('数据库连接失败:', error);
  }
}

testConnection();

export default sequelize;